package com.cr.crandroid.ui.person.approve;

import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.cr.crandroid.R;
import com.cr.crandroid.adapter.ApproveAdapter;
import com.cr.crandroid.adapter.PublishAdapter;
import com.cr.crandroid.bean.Car;
import com.cr.crandroid.ui.person.user.LoginViewModel;
import com.cr.crandroid.utils.CommonConstant;
import com.cr.crandroid.utils.OConvertUtils;
import com.cr.crandroid.utils.OkHttpManager;
import com.cr.crandroid.vo.CarVo;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ApprovedFragment extends Fragment {

    private ApproveAdapter approveAdapter;
    private List<CarVo> cars = new ArrayList<>();
    private CarVo car;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_un_approve, container, false);
        MaterialAlertDialogBuilder materialAlertDialog = new MaterialAlertDialogBuilder(getContext())
                .setTitle("审核发布");
        approveAdapter = new ApproveAdapter(null, materialAlertDialog, inflater, container, getContext());
        materialAlertDialog.setNeutralButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                return;
            }
        });
        materialAlertDialog.setNegativeButton("通过", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                car = approveAdapter.getNowCar();
                car.setStatus(CommonConstant.APPROVE_STATUS_2);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpManager.postJ(CommonConstant.URL + "/car/edit", new Gson().toJson(car), new Callback() {
                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                                String res = response.body().string();
                                JsonObject result = JsonParser.parseString(res).getAsJsonObject();
                                if (Integer.parseInt(String.valueOf(result.get("code"))) != CommonConstant.ERROR) {
                                    if (result.get("result") != null) {
                                        getActivity().runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                getCarVosList();
                                                Snackbar.make(getView(), "通过申请成功",
                                                        Snackbar.LENGTH_LONG).show();
                                            }
                                        });
                                    }
                                }
                            }

                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {

                            }
                        });


                    }
                }).start();
            }
        });
        materialAlertDialog.setPositiveButton("拒绝", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                car = approveAdapter.getNowCar();
                car.setStatus(CommonConstant.APPROVE_STATUS_3);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpManager.postJ(CommonConstant.URL + "/car/edit", new Gson().toJson(car), new Callback() {
                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                                String res = response.body().string();
                                JsonObject result = JsonParser.parseString(res).getAsJsonObject();
                                if (Integer.parseInt(String.valueOf(result.get("code"))) != CommonConstant.ERROR) {
                                    if (result.get("result") != null) {
                                        getActivity().runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                getCarVosList();
                                                Snackbar.make(getView(), "拒绝申请成功",
                                                        Snackbar.LENGTH_LONG).show();
                                            }
                                        });
                                    }
                                }
                            }

                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {

                            }
                        });


                    }
                }).start();
            }
        });
        RecyclerView recyclerView = root.findViewById(R.id.recyclerView);
        recyclerView.setAdapter(approveAdapter);
        View emptyCar = inflater.inflate(R.layout.empty_car, container, false);
        approveAdapter.setEmptyView(emptyCar);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        getCarVosList();
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        getCarVosList();
    }

    private void getCarVosList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<String, String> map = new HashMap<>();
                map.put("status", CommonConstant.APPROVE_STATUS_0 + "," + CommonConstant.APPROVE_STATUS_1);
                OkHttpManager.get(CommonConstant.URL + "/car/queryApproveCar", map, new Callback() {
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String res = response.body().string();
                        JsonObject result = JsonParser.parseString(res).getAsJsonObject();
                        if (Integer.parseInt(String.valueOf(result.get("code"))) != CommonConstant.ERROR) {
                            if (result.get("result") != null) {
                                cars = new Gson().fromJson(result.get("result"), new TypeToken<List<CarVo>>() {
                                }.getType());
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        approveAdapter.setList(cars);
                                    }
                                });

                            }
                        }
                    }

                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }
                });
            }
        }).start();

    }
}